<template>
  <view class="detail-container">
    <!-- 返回按钮 -->
    <view class="back-button" @click="goBack">
      <text class="back-icon">←</text>
      <text class="back-text">返回</text>
    </view>
    
    <!-- 商品详情 -->
    <view class="product-detail" v-if="product">
      <!-- 商品图片 -->
      <view class="image-section">
        <image :src="product.imageUrl" class="product-image" mode="aspectFill"></image>
      </view>
      
      <!-- 商品信息 -->
      <view class="info-section">
        <text class="product-name">{{ product.name }}</text>
        <text class="product-price">¥{{ product.price }}</text>
        <view class="product-status">
          <text class="status-label">商品状态：</text>
          <text class="status-value">{{ getProductStatus() }}</text>
        </view>
      </view>
      
      <!-- 商品描述 -->
      <view class="description-section">
        <text class="section-title">商品描述</text>
        <text class="description-text">{{ product.description }}</text>
      </view>
      
      <!-- 商品参数 -->
      <view class="specs-section">
        <text class="section-title">商品参数</text>
        <view class="specs-item" v-for="(spec, index) in productSpecs" :key="index">
          <text class="spec-label">{{ spec.label }}：</text>
          <text class="spec-value">{{ spec.value }}</text>
        </view>
      </view>
      
      <!-- 卖家信息 -->
      <view class="seller-section">
        <text class="section-title">卖家信息</text>
        <view class="seller-info">
          <text class="seller-name">{{ sellerInfo.name }}</text>
          <text class="seller-rating">评分：{{ sellerInfo.rating }}</text>
        </view>
      </view>
    </view>
    
    <!-- 底部操作栏 - 只保留联系卖家 -->
    <view class="action-bar">
      <view class="contact-button" @click="contactSeller">
        <text class="contact-icon">📞</text>
        <text class="contact-text">联系卖家</text>
      </view>
    </view>
    
    <!-- 联系卖家弹窗 -->
    <view class="modal" v-if="showContactModal">
      <view class="modal-content">
        <text class="modal-title">卖家联系方式</text>
        <text>电话: {{ sellerInfo.phone }}</text>
        <text>微信: {{ sellerInfo.wechat }}</text>
        <view class="modal-buttons">
          <button class="call-button" @click="makePhoneCall(sellerInfo.phone)">一键拨打</button>
          <button class="close-button" @click="closeModal">关闭</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { getConsignmentDetail } from '@/api/getApiResult.js';

// 商品详情
const product = ref(null);
const showContactModal = ref(false);

// 卖家信息
const sellerInfo = ref({
  name: '优质酒商',
  rating: '4.9',
  phone: '13888888888',
  wechat: 'wechat123'
});

// 商品参数
const productSpecs = computed(() => {
  if (!product.value) return [];
  
  // 根据商品名称解析年份、品牌等信息
  const nameParts = product.value.name.split(/(\d+年|珍藏|典藏|特酿|窖藏|限量|特级|顶级|尊享)/g).filter(Boolean);
  
  const specs = [
    { label: '品牌', value: nameParts[0] || '未知' },
    { label: '年份', value: product.value.name.match(/\d+年/) ? product.value.name.match(/\d+年/)[0] : '未知' },
    { label: '类型', value: nameParts.length > 1 ? nameParts.slice(1).join(' ') : '标准版' },
    { label: '容量', value: '500ml' },
    { label: '酒精度', value: '53°' }
  ];
  
  return specs;
});

// 获取商品状态
const getProductStatus = () => {
  if (!product.value) return '';
  
  const description = product.value.description || '';
  if (description.includes('全新')) return '全新';
  if (description.includes('9成新')) return '9成新';
  if (description.includes('8成新')) return '8成新';
  if (description.includes('95%新')) return '95%新';
  return '二手';
};

// 加载详情
const loadDetail = async (id) => {
  try {
    const res = await getConsignmentDetail(id);
    // 支持 {code:0,data:{...}} 或直接对象
    const data = (res && typeof res === 'object' && 'data' in res) ? res.data : res;
    if (!data) throw new Error('详情数据为空');
    
    product.value = {
      id: data.id || id,
      name: data.name || '未命名',
      price: data.price || 0,
      imageUrl: (Array.isArray(data.image) && data.image.length > 0) ? data.image[0] : 'https://picsum.photos/600/400?random=' + Math.random(),
      description: data.description || ''
    };
    
    // 更新卖家联系方式
    sellerInfo.value = {
      name: '寄售用户',
      rating: '5.0',
      phone: data.contactTel || '13888888888',
      wechat: 'wechat123'
    };
  } catch (error) {
    console.error('加载寄售详情失败:', error);
    // 回退默认数据
    product.value = {
      name: '寄售商品',
      price: 0,
      imageUrl: 'https://picsum.photos/600/400?random=' + Math.random(),
      description: '暂无描述'
    };
  }
};

// 接收 id 并加载详情
onLoad((query) => {
  try {
    let id = query?.id;
    console.log('查询参数:', query);
    if (id) loadDetail(id);
  } catch (e) {
    console.error('初始化详情页失败:', e);
  }
});

// 返回上一页
const goBack = () => {
  uni.navigateBack();
};

// 联系卖家
const contactSeller = () => {
  showContactModal.value = true;
};

// 关闭弹窗
const closeModal = () => {
  showContactModal.value = false;
};

// 拨打电话
const makePhoneCall = (phoneNumber) => {
  uni.makePhoneCall({
    phoneNumber: phoneNumber,
  });
};

// 立即购买
const buyNow = () => {
  uni.showToast({
    title: '购买功能开发中',
    icon: 'none'
  });
};
</script>

<style scoped>
.detail-container {
  position: relative;
  padding-bottom: 120rpx; /* 为底部操作栏留出空间 */
  background-color: #f8f8f8;
  min-height: 100vh;
}

/* 返回按钮 */
.back-button {
  position: fixed;
  top: 20rpx;
  left: 20rpx;
  z-index: 10;
  display: flex;
  align-items: center;
  background-color: rgba(255, 255, 255, 0.8);
  padding: 10rpx 20rpx;
  border-radius: 30rpx;
}

.back-icon {
  font-size: 36rpx;
  margin-right: 6rpx;
}

.back-text {
  font-size: 28rpx;
}

/* 商品图片 */
.image-section {
  width: 100%;
  height: 750rpx;
  background-color: #fff;
}

.product-image {
  width: 100%;
  height: 100%;
}

/* 商品信息 */
.info-section {
  background-color: #fff;
  padding: 40rpx 30rpx;
  margin-bottom: 20rpx;
  border-radius: 12rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.product-name {
  font-size: 38rpx;
  font-weight: bold;
  color: #222;
  margin-bottom: 24rpx;
  line-height: 1.4;
}

.product-price {
  font-size: 52rpx;
  color: #ff4d4f;
  font-weight: bold;
  margin-bottom: 24rpx;
}

.product-status {
  display: flex;
  align-items: center;
  margin-top: 16rpx;
  background-color: #f9f9f9;
  padding: 12rpx 16rpx;
  border-radius: 8rpx;
}

.status-label {
  font-size: 28rpx;
  color: #666;
}

.status-value {
  font-size: 28rpx;
  color: #222;
  font-weight: bold;
}

/* 商品描述 */
.description-section, .specs-section, .seller-section {
  background-color: #fff;
  padding: 36rpx 30rpx;
  margin-bottom: 24rpx;
  border-radius: 12rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #222;
  margin-bottom: 24rpx;
  position: relative;
  padding-left: 24rpx;
}

.section-title::before {
  content: '';
  position: absolute;
  left: 0;
  top: 6rpx;
  width: 8rpx;
  height: 32rpx;
  background-color: #ff4d4f;
  border-radius: 4rpx;
}

.description-text {
  font-size: 30rpx;
  color: #444;
  line-height: 1.7;
  padding: 10rpx;
}

/* 商品参数 */
.specs-item {
  display: flex;
  margin-bottom: 16rpx;
}

.spec-label {
  font-size: 26rpx;
  color: #999;
  width: 120rpx;
}

.spec-value {
  font-size: 26rpx;
  color: #333;
  flex: 1;
}

/* 卖家信息 */
.seller-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.seller-name, .seller-rating {
  font-size: 28rpx;
  color: #333;
}

/* 底部操作栏 */
.action-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100rpx;
  display: flex;
  background-color: #fff;
  box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.contact-button, .buy-button {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.contact-button {
  flex: 1;
  background-color: #fff;
}

.contact-icon {
  font-size: 32rpx;
  margin-right: 10rpx;
}

.contact-text {
  font-size: 28rpx;
  color: #333;
}

.buy-button {
  flex: 2;
  background-color: #ff4d4f;
}

.buy-text {
  font-size: 30rpx;
  color: #fff;
  font-weight: bold;
}

/* 弹窗 */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 999;
}

.modal-content {
  background-color: #fff;
  padding: 40rpx;
  border-radius: 16rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 80%;
  max-width: 600rpx;
}

.modal-title {
  font-size: 32rpx;
  font-weight: bold;
  margin-bottom: 30rpx;
  color: #333;
}

.modal-content text {
  margin-bottom: 20rpx;
  font-size: 28rpx;
  color: #666;
}

.modal-buttons {
  display: flex;
  width: 100%;
  justify-content: space-between;
  margin-top: 30rpx;
}

.call-button, .close-button {
  flex: 1;
  margin: 0 10rpx;
  height: 80rpx;
  line-height: 80rpx;
  border-radius: 40rpx;
  font-size: 28rpx;
}

.call-button {
  background-color: #1890ff;
  color: white;
}

.close-button {
  background-color: #f5f5f5;
  color: #666;
}
</style>